#include "Precompiled.h"
#include "Renderer.h"

Sandbox::Renderer::Renderer()
{
	PrintToDebugWindowOnly8("Sandbox. Creating renderer.");
	InitializeCriticalSection(&m_lock);

}

Sandbox::Renderer::~Renderer()
{
	PrintToDebugWindowOnly8("Sandbox. Deleting renderer.");

	DeleteCriticalSection(&m_lock);
}

void Sandbox::Renderer::CreateDeviceResources()
{
	PrintToDebugWindowOnly8("Sandbox. Creating device resources.");

	{ // Create rasterizer state
		Rendering::RasterizerStateDescrition rasterDesc;
		ZeroValue(&rasterDesc);

		rasterDesc.AntialiasedLineEnable = false;
		rasterDesc.CullMode = D3D11_CULL_BACK;
		rasterDesc.DepthBias = 0;
		rasterDesc.DepthBiasClamp = 0.0f;
		rasterDesc.DepthClipEnable = true;
		rasterDesc.FillMode = D3D11_FILL_SOLID;
		rasterDesc.FrontCounterClockwise = true;
		//rasterDesc.FrontCounterClockwise = false;
		rasterDesc.MultisampleEnable = false;
		rasterDesc.ScissorEnable = false;
		rasterDesc.SlopeScaledDepthBias = 0.0f;

		Sehnsucht::ThrowIfFailed(m_d3dDevice->CreateRasterizerState1(
			&rasterDesc, &m_rasterizerCcw
			));
	}
	{ // Create alpha on blend state
		Rendering::BlendStateDescrition blendStateDescription;
		ZeroValue(&blendStateDescription);

		// Create an alpha enabled blend state description.
		blendStateDescription.RenderTarget[0].BlendEnable = TRUE;
		blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
		blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
		blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
		blendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
		blendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
		blendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
		blendStateDescription.RenderTarget[0].RenderTargetWriteMask = 0x0f;

		m_d3dDevice->CreateBlendState1(&blendStateDescription, m_blendingEnabled.GetAddressOf()); // !supp win7
	}
}

void Sandbox::Renderer::CreateDeviceIndependentResources()
{
	PrintToDebugWindowOnly8("Sandbox. Creating device independent resources.");

	m_scene.ImportFbx( L"Basic-Skinning.FBX" );
	auto sceneRoot = m_scene.GetRootChild( );
}

void Sandbox::Renderer::CreateWindowSizeDependentResources()
{
	PrintToDebugWindowOnly8("Sandbox. Creating window size dependent resources.");
}

void Sandbox::Renderer::Initialize()
{
	PrintToDebugWindowOnly8("Sandbox. Initializing renderer.");
	EnterCriticalSection(&m_lock);

	DirectXBaseDesktop::Initialize();

	CreateDeviceIndependentResources();
	CreateDeviceResources();
	CreateWindowSizeDependentResources();

	LeaveCriticalSection(&m_lock);
}

void Sandbox::Renderer::UpdateForWindowSizeChange()
{
	PrintToDebugWindowOnly8("Sandbox. Updating renderer on window size changed.");
	EnterCriticalSection(&m_lock);

	DirectXBaseDesktop::UpdateForWindowSizeChange();

	m_windowSizeChangedArgs.Dim.y = m_d3dRenderTargetSize.height;
	m_windowSizeChangedArgs.Dim.x = m_d3dRenderTargetSize.width;
	m_windowSizeChangedArgs.Dpi.y = m_dpi;
	m_windowSizeChangedArgs.Dpi.x = m_dpi;

	LeaveCriticalSection(&m_lock);
}